<?php
FLEA::loadClass('TMIS_Controller');
class Controller_App_Mp extends TMIS_Controller{

    function __construct() {
        $this->_modelExample = FLEA::getSingleton('Model_Login');
    }

    function actionVerifyQrcodeBuild(){
        $this->authCheck(0);

        $baseurl = detect_uri_base();
        $url = substr($baseurl, 0,strrpos($baseurl, '/') + 1).'api.php';
        $content = array(
            'uid'       =>$_SESSION['USERID'],
            'uname'     =>$_SESSION['USERNAME'],
            'timestamp' =>time(),
            'serverUrl' =>$url,
        );
        $content['token'] = $this->tokenFormat($content);

        // 生成二维码图片
        $qrcodestr = "http://www.eqinfo.com.cn?c=".json_encode($content);

        FLEA::org('phpqrcode/phpqrcode.php');
        $errorCorrectionLevel = 'L';//容错级别
        $matrixPointSize = 4;//生成图片大小
        $qrcode = QRcode::png($qrcodestr,'base64',$errorCorrectionLevel,$matrixPointSize,2);

        //查找当前绑定状态
        $user = $this->_modelExample->find($_SESSION['USERID']);

        $smarty = & $this->_getView();
        // $smarty->assign('qrcodestr',$qrcodestr);
        $smarty->assign('qrcode',$qrcode);
        $smarty->assign('user',$user);
        $smarty->assign('title','登录手机端二维码');
        $smarty->display('App/Mp/VerfyQrcode.tpl');
    }

    function actionVerifyQrcodeBuildSingle(){
        $this->authCheck(0);

        $baseurl = detect_uri_base();
        $url = substr($baseurl, 0,strrpos($baseurl, '/') + 1).'api.php';
        $content = array(
            'uid'       =>$_SESSION['USERID'],
            'uname'     =>$_SESSION['USERNAME'],
            'timestamp' =>time(),
            'serverUrl' =>$url,
        );
        $content['token'] = $this->tokenFormat($content);

        // 生成二维码图片
        $qrcodestr = "http://www.eqinfo.com.cn?c=".json_encode($content);

        FLEA::org('phpqrcode/phpqrcode.php');
        $errorCorrectionLevel = 'L';//容错级别
        $matrixPointSize = 4;//生成图片大小
        $qrcode = QRcode::png($qrcodestr,'base64',$errorCorrectionLevel,$matrixPointSize,2);

        //查找当前绑定状态
        $user = $this->_modelExample->find($_SESSION['USERID']);

        $smarty = & $this->_getView();
        // $smarty->assign('qrcodestr',$qrcodestr);
        $smarty->assign('qrcode',$qrcode);
        $smarty->assign('user',$user);
        $smarty->assign('title','登录手机端二维码');
        $smarty->display('App/Mp/VerfyQrcode2.tpl');
    }

    //验证和生成二维码的token
    function tokenFormat($param = array()){
        $str = $param['timestamp'].'*'.$param['serverUrl'].'*'.$param['uid'].'*'.$param['uname'];
        return md5($str);
    }

    //解除绑定
    function actionBindCannel(){
        $data = array(
            'id'       =>$_SESSION['USERID'],
            'openid'   =>'',
            'unionid'  =>'',
            'nickname' =>'',
        );
        $res = $this->_modelExample->update($data);
        echo json_encode(array('success'=>$res ,'msg'=>'操作完成'));
    }

}

?>